package com.abel;

import org.apache.ignite.Ignite;
import org.apache.ignite.IgniteCache;
import org.apache.ignite.Ignition;
import org.apache.ignite.cache.query.FieldsQueryCursor;
import org.apache.ignite.cache.query.SqlFieldsQuery;

import java.sql.*;
import java.util.Iterator;
import java.util.List;

/**
 * Hello world!
 *
 */
public class App 
{
    public static void main( String[] args ) throws SQLException, ClassNotFoundException {
        App app = new App();
        //app.start();
        //app.init();
        //app.insertData();
        //app.addData();
        //app.loadData();
        Ignite ignite = Ignition.start();
        app.init();
        app.addData(ignite);
        app.loadData(ignite);

    }

    public void start(){
        // Connecting to the cluster.
        Ignite ignite = Ignition.start();
    }

    public void init() throws ClassNotFoundException, SQLException {
        // Register JDBC driver.
        Class.forName("org.apache.ignite.IgniteJdbcThinDriver");

        // Open JDBC connection.
        Connection conn = DriverManager.getConnection("jdbc:ignite:thin://127.0.0.1/");

        // Create database tables.
        try (Statement stmt = conn.createStatement()) {

            // Create table based on REPLICATED template.
            stmt.executeUpdate("CREATE TABLE City (" +
                    " id LONG PRIMARY KEY, name VARCHAR) " +
                    " WITH \"template=replicated\"");

            // Create table based on PARTITIONED template with one backup.
            stmt.executeUpdate("CREATE TABLE Person (" +
                    " id LONG, name VARCHAR, city_id LONG, " +
                    " PRIMARY KEY (id, city_id)) " +
                    " WITH \"backups=1, affinityKey=city_id\"");

            // Create an index on the City table.
            stmt.executeUpdate("CREATE INDEX idx_city_name ON City (name)");

            // Create an index on the Person table.
            stmt.executeUpdate("CREATE INDEX idx_person_name ON Person (name)");
        }finally {
            conn.close();
        }

    }

    public void addData(Ignite ignite) throws ClassNotFoundException, SQLException {
        // Register JDBC driver
        Class.forName("org.apache.ignite.IgniteJdbcThinDriver");

        // Open JDBC connection
        Connection conn = DriverManager.getConnection("jdbc:ignite:thin://127.0.0.1/");

        // Populate City table
        try (PreparedStatement stmt =
                     conn.prepareStatement("INSERT INTO City (id, name) VALUES (?, ?)")) {

            stmt.setLong(1, 1L);
            stmt.setString(2, "Forest Hill");
            stmt.executeUpdate();

            stmt.setLong(1, 2L);
            stmt.setString(2, "Denver");
            stmt.executeUpdate();

            stmt.setLong(1, 3L);
            stmt.setString(2, "St. Petersburg");
            stmt.executeUpdate();
        }

    }

    public void insertData(Ignite ignite){
        // Connecting to the cluster.
        //Ignite ignite = Ignition.start();
        // Getting a reference to an underlying cache created for City table above.
        IgniteCache<Long, City> cityCache = ignite.cache("SQL_PUBLIC_CITY");

        // Inserting entries into City.
        SqlFieldsQuery query = new SqlFieldsQuery(
                "INSERT INTO City (id, name) VALUES (?, ?)");

        cityCache.query(query.setArgs(7, "Forest Hill")).getAll();
        cityCache.query(query.setArgs(8, "Denver")).getAll();
        cityCache.query(query.setArgs(9, "St. Petersburg")).getAll();
        //ignite.close();
    }

    public void loadData(Ignite ignite){
        // Connecting to the cluster.
        //Ignite ignite = Ignition.start();

        // Getting a reference to an underlying cache created for City table above.
        IgniteCache<Long, City> cityCache = ignite.cache("SQL_PUBLIC_CITY");

        // Querying data from the cluster using a distributed JOIN.
        SqlFieldsQuery query = new SqlFieldsQuery("SELECT id, name from City ORDER BY id ASC");

        FieldsQueryCursor<List<?>> cursor = cityCache.query(query);

        Iterator<List<?>> iterator = cursor.iterator();

        while (iterator.hasNext()) {
            List<?> row = iterator.next();
            System.out.println(row.get(0) + ", " + row.get(1));
        }
        ignite.close();
    }
}
